package com.ruoyi.source.controller;

import javax.servlet.http.HttpServletResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.source.api.domain.SrcCropInfo;
import com.ruoyi.source.api.domain.SrcLogistics;
import com.ruoyi.source.service.ISrcCropInfoService;
import com.ruoyi.system.api.domain.SysDept;

import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * 农作物信息Controller
 * 
 * @author ruoyi
 * @date 2024-07-22
 */
@RestController
@Slf4j
@CrossOrigin
@RequestMapping("/crop")
public class SrcCropInfoController extends BaseController {
    @Autowired
    private ISrcCropInfoService srcCropInfoService;
    @Value(("${xxl.job.admin.addresses}"))
    private String test;
    @RequestMapping("text")
    public String test(){
        return test;
    }
    /**
     * 查询农作物信息列表
     */
    @RequiresPermissions("source:crop:list")
    @GetMapping("/list")
    public TableDataInfo selectSrcCropInfoList(@RequestParam(value = "cropName",required = false) String cropName,
                              @RequestParam(value = "cropType",required = false) Long cropType,
                              @RequestParam(value = "plantingWay",required = false)Long plantingWay,
                              @RequestParam(value = "plantingYear",required = false) String plantingYear,
                              @RequestParam(value = "pageNum") Integer pageNum, @RequestParam(value = "pageSize") Integer pageSize) {
        IPage<SrcCropInfo> iPage = new Page<>();
        iPage.setCurrent(pageNum);
        iPage.setSize(pageSize);
        List<SrcCropInfo> list = srcCropInfoService.selectSrcCropInfoList(iPage,cropName,cropType,plantingWay,plantingYear);
        return getDataTable(list,iPage.getTotal());
    }

    /**
     * 导出农作物信息列表
     */
    @RequiresPermissions("source:crop:export")
    @Log(title = "农作物信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrcCropInfo srcCropInfo) {
    /*    IPage<SrcCropInfo> iPage = new Page<>();
        iPage.setCurrent(1);
        iPage.setSize(-1);
        List<SrcCropInfo> list = srcCropInfoService.selectSrcCropInfoList(iPage,srcCropInfo);
        ExcelUtil<SrcCropInfo> util = new ExcelUtil<SrcCropInfo>(SrcCropInfo.class);
        util.exportExcel(response, list, "农作物信息数据");*/
    }

    /**
     * 获取农作物信息详细信息
     */
    @RequiresPermissions("source:crop:query")
    @GetMapping(value = "/{cropId}")
    public AjaxResult getInfo(@PathVariable("cropId") Long cropId) {
        return success(srcCropInfoService.selectSrcCropInfoByCropId(cropId));
    }

    /**
     * 新增农作物信息
     */
    @RequiresPermissions("source:crop:add")
    @Log(title = "农作物信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrcCropInfo srcCropInfo) {
        return toAjax(srcCropInfoService.insertSrcCropInfo(srcCropInfo));
    }

    /**
     * 修改农作物信息
     */
    @RequiresPermissions("source:crop:edit")
    @Log(title = "农作物信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcCropInfo srcCropInfo) {
        return toAjax(srcCropInfoService.updateSrcCropInfo(srcCropInfo));
    }

    /**
     * 删除农作物信息
     */
    @RequiresPermissions("source:crop:remove")
    @Log(title = "农作物信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cropIds}")
    public AjaxResult remove(@PathVariable Long[] cropIds) {
        return toAjax(srcCropInfoService.deleteSrcCropInfoByCropIds(cropIds));
    }

    /**
     * 地区三级联动
     */
    @GetMapping("/address")
    public TableDataInfo selectAddress(){
        return getDataTable(srcCropInfoService.selectAddress());
    }

    /**
     * 上传文件
     */
    @PostMapping("/file/upload")
    public AjaxResult upload(MultipartFile file) throws Exception {
        String s = srcCropInfoService.uploadFile(file);
        return AjaxResult.success(s);
    }
    /**
     * 添加入库减库存
     * @param srcLogistics
     * @return
     */
    @RequestMapping("/toruku")
    public AjaxResult toruku(@RequestBody SrcLogistics srcLogistics){
        int i=   srcCropInfoService.toadd(srcLogistics);
        return AjaxResult.success();
    }

    @GetMapping("/batchInsert")
    public AjaxResult batchInsert(){
        return srcCropInfoService.batchInsert();
    }
    @GetMapping("/deptList")
    public AjaxResult deptList(){
        List<SysDept> deptList= srcCropInfoService.deptList();
        return AjaxResult.success(deptList);
    }
    /**
     * 判断当前时间是否大于种植时间 是则该状态
     */
    @XxlJob("demoJobHandlerss")
    public void updateupdate(){
        List<SrcCropInfo> list = srcCropInfoService.list();
        for (SrcCropInfo srcCropInfo : list) {
            Date plantEndTime = srcCropInfo.getPlantEndTime();
            Date date = new Date();
            if(date.compareTo(plantEndTime)>=0){
                srcCropInfo.setStatus("1");
                srcCropInfoService.updateById(srcCropInfo);
                log.info(srcCropInfo.getCropName()+"已成熟，状态已修改 可以安排采摘了");
            }else{
                log.info(srcCropInfo.getCropName()+"未成熟，等待成熟");
            }
        }
    }
    @PostMapping("updatestatus")
    public AjaxResult updatestatus(@RequestBody SrcCropInfo srcCropInfo){
        SrcCropInfo byId = srcCropInfoService.getById(srcCropInfo.getCropId());
        byId.setShidelivery(srcCropInfo.getShidelivery());
        byId.setStatus("0");
        boolean b = srcCropInfoService.updateById(byId);
        return AjaxResult.success();
    }
}
